package com.starlinkdt.service.system.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starlinkdt.common.domain.system.entity.TaskDispatch;
import com.starlinkdt.common.enums.TaskStatusEnum;
import com.starlinkdt.common.mapper.system.TaskDispatchMapper;
import com.starlinkdt.service.system.ITaskDispatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service("TaskDispatchServiceImpl")
public class TaskDispatchServiceImpl extends ServiceImpl<TaskDispatchMapper, TaskDispatch> implements ITaskDispatchService {

    @Autowired
    private TaskDispatchMapper taskMapper;

    /**
     * 分页获取待处理任务
     */
    public List<TaskDispatch> getPendingTasks(int pageSize, int taskType, String businessType) {
        return taskMapper.getPendingTasks(pageSize, taskType, businessType);
    }

    /**
     * 分页获取失败且可重试的任务
     */
    @Override
    public List<TaskDispatch> getFailedTasksForRetry(int pageNum, int pageSize, int taskType, String businessType, int tryTimes) {
        int offset = (pageNum - 1) * pageSize;
        return taskMapper.getFailedTasksForRetry(pageSize, offset, taskType, businessType, tryTimes);
    }

    /**
     * 分页获取长期处于进程中的任务
     */
    @Override
    public List<TaskDispatch> getProcessingTasksForRetry(int pageSize, int taskType, String businessType) {
        return taskMapper.getProcessingTasksForRetry(pageSize, taskType, businessType);
    }

    /**
     * 批量标记任务成功
     */
    @Transactional
    public boolean batchMarkAsSuccess(List<TaskDispatch> tasks) {
        return taskMapper.batchMarkAsSuccess(tasks) > 0;
    }

    /**
     * 批量标记任务失败
     */
    @Transactional
    public boolean batchMarkAsFailed(List<TaskDispatch> tasks) {
        return taskMapper.batchMarkAsFailed(tasks) > 0;
    }

    /**
     * 批量标记任务正在执行
     *
     * @return
     */
    @Transactional
    public boolean batchMarkAsProcessing(List<TaskDispatch> tasks) {
        return taskMapper.batchMarkAsProcessing(tasks) > 0;
    }


    /**
     * 批量标记任务未处理
     *
     * @param tasks
     * @return
     */
    @Transactional
    public boolean batchMarkAsNoHandled(List<TaskDispatch> tasks) {
        return taskMapper.batchMarkAsNoHandled(tasks) > 0;
    }


    /**
     * 批量标记任务待处理
     *
     * @param tasks
     * @return
     */
    @Transactional
    public boolean batchMarkAsPending(List<TaskDispatch> tasks) {
        return taskMapper.batchMarkAsPending(tasks) > 0;
    }

    /**
     * 判断是否存在待处理的任务
     *
     * @param businessId
     * @return
     */
    @Override
    public boolean existsPendingTask(String businessId) {
        return taskMapper.existsPendingTask(businessId) > 0;
    }

    /**
     * @param businessId
     * @return
     */
    public Optional<TaskDispatch> getLatestFailedTask(String businessId) {
        TaskDispatch latestTask = taskMapper.getLatestTaskByBusinessId(businessId);
        if (latestTask != null && latestTask.getStatus() == TaskStatusEnum.FAIL.getCode()) {
            return Optional.of(latestTask);
        }
        return Optional.empty();
    }


}
